<template>
  <avue-crud :data="logsList"
             :table-loading="loading"
             :option="option">
    <template slot="menuLeft">
      <el-button type="primary"
                 size="small"
                 icon="el-icon-upload"
                 @click="send">上传服务器</el-button>
      <el-button type="danger"
                 size="small"
                 icon="el-icon-delete"
                 @click="clear">清空本地日志</el-button>
    </template>
    <template slot-scope="scope"
              slot="type">
      <el-tag type="danger"
              size="small">{{scope.label}}</el-tag>
    </template>
    <template slot-scope="props"
              slot="expand">
      <pre class="code">
        {{props.row.stack}}
      </pre>
    </template>
  </avue-crud>
</template>

<script>
import {exportSysErrorLogExcel,saveOrUpdateSysErrorLog,saveOrUpdateSysErrorLogList,saveSysErrorLog,saveSysErrorLogList,deleteSysErrorLog,querySysErrorLog,querySysErrorLogListPage,updateSysErrorLog,deleteSysErrorLogList} from '@/api/system/sysErrorLogApi';
import {mapGetters} from 'vuex';
import { reqCommonMsg } from '@/util/reqCommonMsg';
import {filterFormat} from '@/util/util';
export default {
  name: 'errLogs',
  data() {
    return {
      loading: false, 
      option: {
        menu: false,
        addBtn: false,
        page: false,
        border: true,
        expand: true,
        refreshBtn: false,
        headerAlign: 'center',
        column: [
          {
            label: '类型',
            prop: 'type',
            width: 80,
            align: 'center',
            solt: true,
            dicData: [
              {
                label: 'bug',
                value: 'error'
              }
            ]
          },
          {
            label: '地址',
            width: 200,
            prop: 'url',
            overHidden: true
          },
          {
            label: '内容',
            prop: 'message',
            overHidden: true
          },
          {
            label: '错误堆栈',
            prop: 'stack',
            hide: true
          },
          {
            label: '时间',
            align: 'center',
            prop: 'time',
            width: 200
          }
        ]
      },
      data: [],
      // 请求参数区
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      reqParams: {
        singleBody: {},
        listBody: [{}],
        sqlCondition:[],
        orderRule: [],
        currentPage: 1,
        pageSize: 10
      },
      commonData: null,
      conditions: [],
      orderRules: [],
      singleBody: {}
    };
  },
  created() {},
  mounted() {},
  computed: {
    ...mapGetters(['logsList'])
  },
  props: [],
  methods: {
    send() {
      this.$confirm('确定上传本地日志到服务器?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true;
          //批量写入记录
          let dataList = [];
          this.logsList.forEach(element => {
            dataList.push(
              {
                errorType: element.type,
                errorAddress: element.url,
                errorSummary: element.message,
                errorContent: element.stack,
                errorDatetime: element.time
              }
            )
          });
          this.reqParams.listBody = dataList;
          this.commonData = reqCommonMsg(this.reqParams)
          saveSysErrorLogList(this.commonData).then(() => {
            this.$message({
              type: 'success',
              message: '上传成功!'
            });
            this.loading = false;
          }, error => {
            this.$console.log(error);
            this.loading = false;
          }); 
        })
        .catch(() => {});
    },
    clear() {
      this.$confirm('确定清空本地日志记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('CLEAR_LOGS');
          this.$console.log(this);
          this.$parent.$parent.box = false;
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '清空成功!'
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.code {
  font-size: 12px;
  display: block;
  font-family: monospace;
  white-space: pre;
  margin: 1em 0px;
}
</style>
